package com.stox.client.ui.javafx.chartingy.model.node;

import javafx.scene.paint.Paint;
import javafx.scene.shape.Line;

import com.stox.client.ui.javafx.chartingy.model.axis.DateAxis;
import com.stox.client.ui.javafx.chartingy.model.plot.Data;
import com.stox.client.ui.javafx.chartingy.model.plot.PricePlot;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.shared.charting.model.Kagi;
import com.stox.shared.core.model.Bar;
import com.stox.shared.core.model.Scrip;

public class KagiNode extends AbstractPriceNode {

	private final Line lower = new Line();
	private final Line upper = new Line();
	private final Line connector = new Line();

	public KagiNode(final Data<Scrip, Bar> data) {
		super(data);
		UI.style(this, CSS.kagiNode);
		final Kagi kagi = (Kagi) getData().getExtraValue();

		lower.startXProperty().bindBidirectional(lower.endXProperty());
		upper.startXProperty().bindBidirectional(upper.endXProperty());
		connector.startYProperty().bindBidirectional(connector.endYProperty());

		upper.startXProperty().bindBidirectional(lower.endXProperty());
		connector.endXProperty().bindBidirectional(lower.endXProperty());
		connector.endYProperty().bindBidirectional(kagi.isUp() ? lower.startYProperty() : upper.endYProperty());
		upper.startYProperty().bindBidirectional(lower.endYProperty());

		connector.strokeWidthProperty().bindBidirectional(lower.strokeWidthProperty());
		lower.strokeWidthProperty().bindBidirectional(upper.strokeWidthProperty());

		getChildren().addAll(lower, upper, connector);
		updateColor();
	}

	@Override
	public void updateColor() {
		final Kagi kagi = (Kagi) getData().getExtraValue();
		final PricePlot plot = (PricePlot)getData().getPlot();
		Paint upColor = plot.getUpBarColor();
		Paint downColor = plot.getDownBarColor();
		connector.setStroke(kagi.isPreviousTrendUp() ? upColor : downColor);
		if (kagi.isUp()) {
			if (kagi.isPreviousTrendUp()) {
				lower.setStroke(upColor);
			} else {
				lower.setStroke(downColor);
			}
			if (kagi.isNextTrendUp()) {
				upper.setStroke(upColor);
			} else {
				upper.setStroke(downColor);
			}
		} else {
			if (kagi.isPreviousTrendUp()) {
				upper.setStroke(upColor);
			} else {
				upper.setStroke(downColor);
			}
			if (kagi.isNextTrendUp()) {
				lower.setStroke(upColor);
			} else {
				lower.setStroke(downColor);
			}
		}
	}

	@Override
	public void layoutChartChildren() {
		final DateAxis dateAxis = getData().primaryChart().getDateAxis();
		final Kagi kagi = (Kagi) getData().getExtraValue();

		final double x = dateAxis.getDisplayPosition(getData().getIndex());
		final double unitWidth = dateAxis.getUnitWidth();
		connector.setStrokeWidth(unitWidth > 10 ? unitWidth / 10 : 1);
		connector.setStartX(x - unitWidth);
		connector.setEndX(x);
		lower.setStartY(getDisplayPosition(kagi.getLow()));
		lower.setEndY(getDisplayPosition(0 < kagi.getPreviousReversalPrice() ? kagi.getPreviousReversalPrice() : kagi.getLow()));
		upper.setEndY(getDisplayPosition(kagi.getHigh()));
	}
}
